Top > Ruby [2.2.2] > Hash( [] , []= )


Hash# [] , []=

runrun = {:acha=>0, :porute=>1, :piipo=>2}
p runrun[:acha]        # 1
p runrun[:achacha]     # nil
p runrun[:porute,:piipo]
         # `[]': wrong number of arguments(2 for 1) (ArgumentError)

ranran = {0=>"acha", 1=>"porute", 2=>"piipo"}
p ranran[1]         # "porute"
p ranran[-1]        # nil
p ranran[0,2]
         # `[]': wrong number of arguments(2 for 1) (ArgumentError)

p runrun[:porute] = 666        # 666
p runrun[:achacha] = 4         # 4
p runrun         # {:acha=>0, :porute=>666, :piipo=>2, :achacha=>4}

ハッシュってIDと名前とか郵便番号と住所とか結構便利ですよね。
更新の場合、追加の場合でも簡単に対応できます。



Copyright © 2015 AchaPorutePiipo All Rights Reserved.